package system

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SystemOrderApi struct{}

// GetOrderList
// @Tags      SysOrder
// @Summary   分页获取订单列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      systemReq.SearchOrderParams                        true  "分页获取订单列表"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}  "分页获取订单列表,返回包括列表,总数,页码,每页数量"
// @Router    /order/getOrderList [post]
func (s *SystemOrderApi) GetOrderList(c *gin.Context) {
	var pageInfo systemReq.SearchOrderParams
	var list []systemRes.SystemOrderListPageResponseItem
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(pageInfo.PageInfo, utils.PageInfoVerify)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := orderService.GetOrderInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetOrderByOrderNo
// @Tags      SysOrder
// @Summary   根据id获取订单
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      systemReq.SysOrderReq                                   true  "根据id获取订单"
// @Success   200   {object}  response.Response{data=systemRes.SysOrderResponse}  "根据id获取订单,返回包括订单详情"
// @Router    /order/getOrderById [post]
func (s *SystemOrderApi) GetOrderByOrderNo(c *gin.Context) {
	var orderDetailReq systemReq.SysOrderReq
	err := c.ShouldBindUri(&orderDetailReq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	order, err := orderService.GetOrderByOrderNo(orderDetailReq.OrderNo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(systemRes.SysOrderResponse{Order: order}, "获取成功", c)
}

// GetAllOrders
// @Tags      SysOrder
// @Summary   获取所有的订单 不分页
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Success   200  {object}  response.Response{data=systemRes.SysOrderListResponse,msg=string}  "获取所有的订单 不分页,返回包括订单列表"
// @Router    /order/getAllOrders [post]
func (s *SystemOrderApi) GetAllOrders(c *gin.Context) {
	orders, err := orderService.GetAllOrders()
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(systemRes.SysOrderListResponse{Orders: orders}, "获取成功", c)
}

// UpdateOrderStatus
// @Tags      SysOrder
// @Summary   更新订单状态
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      systemReq.UpdateOrderStatusReq  true  "更新订单状态"
// @Success   200   {object}  response.Response{msg=string}   "更新订单状态"
// @Router    /order/updateOrderStatus [post]
func (s *SystemOrderApi) UpdateOrderStatus(c *gin.Context) {
	var req systemReq.UpdateOrderStatusReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	// 验证状态值 (1 待支付 2 制作中 3 已发货 4 已完成 5 已取消)
	validStatuses := []int64{1, 2, 3, 4, 5}
	validStatus := false
	for _, status := range validStatuses {
		if req.Status == status {
			validStatus = true
			break
		}
	}
	if !validStatus {
		response.FailWithMessage("无效的订单状态", c)
		return
	}

	err = orderService.UpdateOrderStatus(req)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败: "+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// GetOrderStats
// @Tags      SysOrder
// @Summary   获取订单统计
// @accept    application/json
// @Produce   application/json
// @Success   200   {object}  response.Response{data=systemRes.OrderStatsResponse,msg=string}  "获取订单统计"
// @Router    /order/getOrderStats [get]
func (s *SystemOrderApi) GetOrderStats(c *gin.Context) {
	stats, err := orderService.GetOrderStats()
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}
	response.OkWithDetailed(stats, "获取成功", c)
}
